package com.ruoyi.framework.config.i18n;

import com.ruoyi.common.utils.StringUtils;
import org.springframework.web.servlet.LocaleResolver;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Locale;

public class I18nLocaleResolver implements LocaleResolver
{

    @Override
    public Locale resolveLocale(HttpServletRequest httpServletRequest)
    {
        String parameter = httpServletRequest.getParameter("language");
        if (StringUtils.isEmpty(parameter))
        {
            parameter = httpServletRequest.getHeader("language");
        }
        //获取请求参数
        //获取默认的locale
        Locale locale = Locale.getDefault();
        if (!StringUtils.isEmpty(parameter))
        {
            String[] strings = parameter.split("-");
            //若请求参数不为空，则根据请求构造一个新的locale
            locale = new Locale(strings[0], strings[1]);
        }
        //否则返回默认的locale
        return locale;
    }


    @Override
    public void setLocale(HttpServletRequest request, HttpServletResponse response, Locale locale)
    {

    }
}
